# 定义使用的各种工具类
from functools import wraps

from flask import session, current_app, jsonify, g
from first_info.models import User
from first_info import response_code

def do_rank(index):
    if index == 1:
        return "first"
    elif index == 2:
        return "second"
    elif index == 3:
        return "third"
    else:
        return ""

# 定义装饰器函数

def user_login_data(view_func):
    # 还原装饰器修改后的__name__，还有被装饰的函数中的描述信息
    @wraps(view_func)
    def wrapper(*args, **kwargs):
        user_id = session.get('user_id', None)
        user = None
        if user_id:
            try:
                user = User.query.get(user_id)
            except Exception as e:
                current_app.logger.error(e)
                return jsonify(errno=response_code.RET.DBERR, errmag='未查询到记录')
        g.user = user
        return view_func(*args, **kwargs)

    return wrapper
